package domain;

import java.util.Date;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import dal.ConnectionManager;


public class Resource {
	
	/**Every resource has a unique id*/
    private Integer resourceId;
    /**Name of the resource*/
	private String name;
	/**Starting day of the resource*/
	private Date startDate = null;
	/**Hours of the resource*/
	private Integer hours; 
	private Task myTask;
	private ArrayList<Resource> myResourceList = new ArrayList<Resource>();
	
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	ConnectionManager mgr = ConnectionManager.getInstance();
	Connection con = mgr.getConnection();
	Statement st;
	
	/**
	 * Empty constructor of Resource
	 */
	public Resource(){
		
	}
	
	/**
	 * Constructor of Resource
	 * @param name Name of the resource
	 * @param dateInput 
	 * @param hours Hours of resource 
	 */
	public Resource(String name, String dateInput, Integer hours) {
		super();
		this.name = name;
		setStartDate(dateInput);
		this.hours = hours;
	}

	
	/**
	 * Add new resource to resource table
	 * @param myResource Object of resource
	 */
	public void addResource(Resource myResource) {
		// TODO Auto-generated method stub
		
		try {
			Statement st = con.createStatement();
			st.executeUpdate("insert into resource (name, startDate, hours) values ('"+myResource.getName()+"', '"+myResource.getDateToString()+"', "+myResource.getHours()+")");
			ResultSet rs = st.executeQuery("select id from resource where name='"+myResource.getName()+"'");
			while(rs.next())
			{
				myResource.setResourceId(rs.getInt(1));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Remove  resource from resource table
	 * @param myResource Object of resource
	 */
	public void removeResource(Resource myResource) {
		// TODO Auto-generated method stub
		
		try {
			//myTask.unassignResourceToTask(myResource.getMyTask(), myResource.getResourceId());
			Statement st = con.createStatement();
			st.executeUpdate("delete from resource where ID = "+myResource.getResourceId()+"");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}

	/**
	 * Update the information of a specified resource
	 * @param myResource Object of resource
	 * @param resourceId ResourceId of the resource
	 */
	public void updateResource(Integer resourceId, Resource myResource,
			ArrayList<Resource> myResList) {
		// TODO Auto-generated method stub
		try {
			Statement st = con.createStatement();
			st.executeUpdate("update resource set name='"+myResource.getName()+"', startDate='"+myResource.getDateToString()+"', hours="+myResource.getHours()+" where id="+myResource.getResourceId()+"");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * @param myTask
	 * @param resourceId 
	 * @param myTaskList
	 */
	public void deleteResourceFromTasks(int resourceId, Task myTask,
			ArrayList<Task> myTaskList) {
		// TODO Auto-generated method stub
		for(int i=0; i <= myTaskList.size(); i++)
		{
			if(myTaskList.get(i).getResourceId() == resourceId)
			{
				myTask.setResourceId(0);
				myTaskList.set(i, myTask);
			}
		}

	}
	
	/**
	* This method unassigns the resource from it's tasks.
	* @param myTask Object of task
	* @param resourceId ResourceId of the resource
	*/
	public void unassignResourceFromTasks(int resourceId, Task myTask) {
	// TODO Auto-generated method stub

	try{
	Statement st = con.createStatement();
	ResultSet rs = st.executeQuery("select id, resourceId from task where resourceId =" + myTask.getResourceId()+"");
	if(rs.next()){
	do{
	st.execute("update task set resourceId =0 where id=" + rs.getInt(2));
	}while(rs.next());
	} 
	}catch (SQLException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
	}


	}
	
	/**
	 * Assigns resource to task
	 * @param myTask Object of task
	 * @param resourceId resourceId of resource
	 */
	public void assignResourceToTask(Task myTask,Integer resourceId)
	{
		try {
			st.executeUpdate("update task set resourceId="+resourceId+" where id="+myTask.getId());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Destroy the resource from resource table and unassigns this resource from the related tasks
	 * @param myResource Object of resource
	 */
	public void destroyResource(Resource myResource){
		
		try{
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select id from task where resourceId=" + myResource.getResourceId());
			if(rs.next()){
				do{
					
					st.executeQuery("update task set resouceId=0 where id="+ rs.getInt(1));
			
				}while(rs.next());
				
				st.execute("delete from resource where id=" + myResource.getResourceId());
			}
			
		} catch(SQLException e ){
			e.printStackTrace();
		}
		
	}
		
	/**
	 * Getters and Setters
	 * @return
	 */
	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public Date getStartDate() {
		return startDate;
	}


	public void setStartDate(String dateInput) {
		try {
		      startDate = format.parse(dateInput);
		    } catch (ParseException e) {
		      e.printStackTrace();
		    }
	}
	
	public String getDateToString()
	{
		return (format.format(startDate));
	}


	public Integer getHours() {
		return hours;
	}


	public void setHours(Integer hours) {
		this.hours = hours;
	}


	public ArrayList<Resource> getMyResourceList() {
		return myResourceList;
	}

	public void setMyResourceList(ArrayList<Resource> myResourceList) {
		this.myResourceList = myResourceList;
	}

	public void setResourceId(Integer resourceId) {
		this.resourceId = resourceId;
	}

	public Integer getResourceId() {
		return resourceId;
	}

	public void setMyTask(Task myTask) {
		this.myTask = myTask;
	}

	public Task getMyTask() {
		return myTask;
	}
	
	/**
	 * Retrieve the all elements in resource table
	 * @return
	 */
	public ResultSet selectResources()
	{
		ResultSet rs = null;
		
		try {
			rs = st.executeQuery("select * form resource");
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		
		return rs;
	}

	/**
	 * Select the all information of specific Resource from database
	 * @param resourceID
	 * @return
	 */
	public ResultSet selectResourceById(Integer resourceID)
	{
		ResultSet rs = null;
		
		try {
			rs = st.executeQuery("select * form resource where id="+resourceID);
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		
		return rs;
	}
	
	public Resource selectbyTask(Task myTask) 
	{
		try {
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select r.id,r.name,r.startDate,r.hours "+
											"from resource r "+
											"join task t on r.id=t.resourceId "+
											"where t.id="+myTask.getId()+";");
				
			Resource myResource = new Resource();
			if (rs.next()) {
			     do {
			    	 myResource.setResourceId(rs.getInt(1));
			    	 myResource.setName(rs.getString(2));
			    	 myResource.setStartDate(rs.getString(3));
			    	 myResource.setHours(rs.getInt(4));
			       } while (rs.next());
			 	return myResource;  
			     }
			  else {
			    return null;
		}			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	
}
